using System;
using RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Core;

namespace RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Core
{
    /// <summary>
    /// Facade for the loger
    /// </summary>
    public class LogGateway
    {
        private static readonly object _sync = new object();
        private static ILogFactory _logFactory;


        /// <summary>
        /// Initializes a new instance of the <see cref="LogGateway"/> class.
        /// </summary>
        /// <param name="logFactory">The log factory which determinies a concrate logger.</param>
        private LogGateway(ILogFactory logFactory)
        {
            _logFactory = logFactory;
        }

        /// <summary>
        /// Gets an log for a type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static ILog LogFor(Type type)
        {
            if (_logFactory == null)
            {
                return new NullObjectLog();
            }
            return _logFactory.CreateFor(type);
        }


        public static void InitializeWith(ILogFactory logFactory)
        {
            if (logFactory == null) throw new ArgumentNullException("logFactory");
            if (_logFactory != null)
            {
                throw new InvalidOperationException("Can't initailize second time the log gateway");
            }
            if (_logFactory != null) return;
            lock (_sync)
            {
                if (_logFactory == null)
                {
                    _logFactory = logFactory;
                }
            }
        }
    }
}